package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SportDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Sport;
import com.itheima.service.SportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = SportService.class)
@Transactional
public class SportServiceImpl implements SportService {

    @Autowired
    private SportDao sportDao;

    @Override
    public void add(Sport sport) {
        sportDao.add(sport);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //每页记录数
        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();
        //1.分页查询助手查询,基于mybatis框架提供的分页助手,省去sql中limit页码,条数;
        PageHelper.startPage(currentPage, pageSize);
        //2.获取带分页数据对象
        Page<Sport> page = sportDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Sport> rows = page.getResult();//pageSize条数据:10条
        return new PageResult(total, rows);
    }

    @Override
    public void deleteById(Integer id) {
        sportDao.deleteById(id);
    }

    @Override
    public void edit(Sport sport) {
        sportDao.edit(sport);
    }

    @Override
    public Sport findById(Integer id) {
        return sportDao.findById(id);
    }

    @Override
    public List<Sport> findAll() {
        return sportDao.findAll();
    }

    @Override
    public void addList(List<Sport> data) {
        for (Sport sport : data) {
            sportDao.add(sport);
        }
    }
}
